<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>分治法快速排序验证</title>

	<meta charset="utf-8">
<script type="text/javascript">
function myFunction(){
function quickSort(arr, left, right) {
    var len = arr.length,
        partitionIndex,
        left = typeof left != 'number' ? 0 : left,
        right = typeof right != 'number' ? len - 1 : right;

    if (left < right) {
        partitionIndex = partition(arr, left, right);
        quickSort(arr, left, partitionIndex-1);
        quickSort(arr, partitionIndex+1, right);
    }
    return arr;
}

function partition(arr, left ,right) {     // 分区操作
    var pivot = left,                      // 设定基准值（pivot）
        index = pivot + 1;
    for (var i = index; i <= right; i++) {
        if (arr[i] < arr[pivot]) {
            swap(arr, i, index);
            index++;
        }        
    }
    swap(arr, pivot, index - 1);
    return index-1;
}

function swap(arr, i, j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}
function partition2(arr, low, high) {
  let pivot = arr[low];
  while (low < high) {
    while (low < high && arr[high] > pivot) {
      --high;
    }
    arr[low] = arr[high];
    while (low < high && arr[low] <= pivot) {
      ++low;
    }
    arr[high] = arr[low];
  }
  arr[low] = pivot;
  return low;
}

function quickSort2(arr, low, high) {
  if (low < high) {
    let pivot = partition2(arr, low, high);
    quickSort2(arr, low, pivot - 1);
    quickSort2(arr, pivot + 1, high);
  }
  return arr;
}
	var str= document.getElementById("array").value;
	var arr=str.split(",");
	var left=arr[0];
	var right=arr[arr.length];
	var res=quickSort(arr, left, right).join(",");
	document.getElementById("result").value = res;
}
</script>
</head>
<body>
				<label>输入子段元素：</label>
			<input type="text" name="array" id="array" placeholder="请输入数组元素，用英文逗号隔开">
	<p><label>例如：23,45,67,89,16,55,43,22,98,87,76</label><br/><br/>
		<input type="button" onclick="myFunction()" value="计算结果" />
	</p>
	        
	<p>
		<label>输出结果：</label>
	</p>
			
			<input type="text" name="result" id="result">

</body>
</html>
